
// pages/invest/invest.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab: 0,             //选的是第几个标的分类
    tablist:[],          //标的分类列表
    datalist:[],           //标的列表
    pagelist:[],            //每类标是第几页
    totalpagelist:[]        //每种标总页数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.request({    //标的分类接口
      url: getApp().data.baseURL + '/api/noauth/borrow_type_list',
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: "post",
      success: function (res) {
        getApp().data.consoleDebug ? console.log(res.data) : '';
        for (let i = 0; i < res.data.data.length; i++) {
          that.data.pagelist[i] = 0;
          that.data.totalpagelist[i] = -1;
        }
        that.setData({
          tablist: res.data.data,
          pagelist: that.data.pagelist,
          totalpagelist: that.data.totalpagelist
        })
        that.initSrcoll(that);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.initSrcoll(this);
    //wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('123')
    let that = this;
    if (that.data.pagelist[that.data.tab] < that.data.totalpagelist[that.data.tab]){
      wx.request({    //标的列表接口
        url: getApp().data.baseURL + '/api/noauth/investment_list?page=' + that.data.pagelist[that.data.tab] + '&borrowType=' + that.data.tablist[that.data.tab].id,
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: "post",
        success: function (res) {
          getApp().data.consoleDebug ? console.log(res.data) : '';
          that.data.datalist[that.data.tab] = that.data.datalist[that.data.tab].concat(res.data.data.content);
          that.data.totalpagelist[that.data.tab] = res.data.data.totalPages;
          that.setData({
            datalist: that.data.datalist,
            totalpagelist: that.data.totalpagelist
          })
        }
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  initSrcoll: function(that){       //初始化列表
    wx.request({    //标的列表接口
      url: getApp().data.baseURL + '/api/noauth/investment_list?page=' + that.data.pagelist[that.data.tab] + '&borrowType=' + that.data.tablist[that.data.tab].id,
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: "post",
      success: function (res) {
        getApp().data.consoleDebug ? console.log(res.data) : '';
        that.data.datalist[that.data.tab] = res.data.data.content;
        that.data.totalpagelist[that.data.tab] = res.data.data.totalPages;
        that.setData({
          datalist: that.data.datalist,
          totalpagelist: that.data.totalpagelist
        })
        wx.stopPullDownRefresh()
      }
    })
  },

  changeTab: function(options){    //切换tab
    this.setData({
      tab: options.currentTarget.dataset.tabnum
    })
    if (this.data.totalpagelist[this.data.tab]==-1){
      this.initSrcoll(this);
    }
    console.log(this.data)
  }
})